package com.zretc.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

public class Util {

    /**
     * @param inputText 需加密字符串
     * @return 加密后的字符串
     * @author 苏帆
     * Md5加密工具，32位加密
     */
    public static String getMd5(String inputText) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(inputText.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 生成不带’-‘的UUID
     *
     * @author: bLUeSuMMerDreamer
     * @date: 2020/7/24 20:33
     * @return: java.lang.String
     */
    public static String getUUID() {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        return uuid;
    }

    /**
     * 随机生成字符串
     *
     * @param length 设定字符串长度
     * @return 返回随机生成的字符串
     */
    public static String getRandomString(int length) {
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(str.length());
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 在字符串之前加0
     *
     * @param :fillLength
     * @param :youNumber
     * @author: bLUeSuMMerDreamer
     * @date: 2020/7/25 17:18
     * @return: java.lang.String
     */
    public static String fillNumberBeforeString(Integer fillLength, Integer youNumber) {

        return String.format("%0" + fillLength + "d", youNumber);
    }

    public static String fillNumberBeforeString(Integer fillLength, Long youNumber) {

        String.valueOf(youNumber);
        String trim = String.valueOf(youNumber).trim();
        Integer i = Integer.parseInt(trim);
        //return String.formNumberat("%0" + fillLength + "d", youNumber);
        return String.format("%0" + fillLength + "d", i);
    }

    /**
     * 获取orderID
     *
     * @author: bLUeSuMMerDreamer
     * @date: 2020/7/25 17:39
     * @return: java.lang.String
     */
    public static String getOrderId() {
        Long time = System.currentTimeMillis(); // 获取时间戳
        String orderId = "EDS" + getRandomString(5) + time;           // "EDS"简单标识
        return orderId;
    }

    /**
     * 获取trackID
     *
     * @author: bLUeSuMMerDreamer
     * @date: 2020/7/25 17:39
     * @return: java.lang.String
     */
    public static String getTrackId() {
        Long time = System.currentTimeMillis(); // 获取时间戳
        String trackId = "TRA" + getRandomString(5) + time;// "EDS"简单标识
        return trackId;
    }

    /**
     * 生成现在的时间戳，可直接将timestamp直接编译进预编译语句
     *
     * @return Timestamp
     */
    public static String getNowTime() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        return format;
    }

    /**
     * 缩放后裁剪图片方法
     *
     * @param srcImageFile 源文件
     * @param x            x坐标
     * @param y            y坐标
     * @param destWidth    最终生成的图片宽
     * @param destHeight   最终生成的图片高
     * @param finalWidth   缩放宽度
     * @param finalHeight  缩放高度
     */
    public static void abscut(String srcImageFile, Integer x, Integer y, Integer destWidth, Integer destHeight,
                              Integer finalWidth, Integer finalHeight) {
        if (finalWidth != 0 && finalHeight != 0) {
            try {
                Image img;
                ImageFilter cropFilter;
                // 读取源图像
                BufferedImage bi = ImageIO.read(new File(srcImageFile));
                int srcWidth = bi.getWidth(); // 源图宽度
                int srcHeight = bi.getHeight(); // 源图高度

                if (srcWidth >= destWidth && srcHeight >= destHeight) {
                    Image image = bi.getScaledInstance(finalWidth, finalHeight, Image.SCALE_DEFAULT);// 获取缩放后的图片大小
                    cropFilter = new CropImageFilter(x, y, destWidth, destHeight);
                    img = Toolkit.getDefaultToolkit()
                            .createImage(new FilteredImageSource(image.getSource(), cropFilter));
                    BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
                    Graphics g = tag.getGraphics();
                    g.drawImage(img, 0, 0, null); // 绘制截取后的图
                    g.dispose();
                    // 输出为文件
                    ImageIO.write(tag, "JPEG", new File(srcImageFile));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static <T> List<T> GetRandomInfoList(List<T> list, Integer count) {
        List<T> newList = new ArrayList<T>();
        Random r = new Random();
        for (int i = 0; i < count; i++) {
            if (list.size() == 0) {
                return newList;
            }
            int ran1 = r.nextInt(list.size());
            newList.add(list.get(ran1));
            list.remove(ran1);
        }
        return newList;

    }
}
